package com.shd.edanpei.presenter;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.exception.ResponeThrowable;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.bean.MessageModel;
import com.shd.edanpei.bean.PagingModel;
import com.shd.edanpei.db.DaoManager;
import com.shd.edanpei.presenter.contract.HomeSearchContract;
import com.shd.edanpei.util.LayoutUtils;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/12/27.
 * yang-wen-wen
 */

public class HomeSearchPresenter extends BasePresenterImpl<HomeSearchContract.HomeSearchIView>
        implements HomeSearchContract.HomeSearchIPresenter {

    private int currentPage;
    private int totalPage;

    public HomeSearchPresenter(HomeSearchContract.HomeSearchIView mView) {
        super(mView);
    }

    @Override
    public void refresh(String str) {
        currentPage = 1;
        ApiCreator.getApi().articleList(currentPage, DaoManager.getInstance().getUserBean().getId(), str)
                .compose(RxTransformer.<PagingModel<MessageModel>>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<PagingModel<MessageModel>>() {

                    protected void onSuccess(PagingModel<MessageModel> data) {
                        if (data != null && data.getList().size() > 0) {
                            totalPage = data.getTotalPage();
                            mBaseIView.getAdapter().setNewData(data.getList());
                            if (currentPage >= totalPage) {
                                mBaseIView.getAdapter().loadMoreEnd();
                            } else {
                                mBaseIView.getAdapter().loadMoreComplete();
                            }
                            currentPage++;
                        } else {
                            mBaseIView.getAdapter().setNewData(null);
                            mBaseIView.getAdapter()
                                    .setEmptyView(LayoutUtils.getNullLayout(mBaseIView.getContext(), R.drawable.null_message, "暂无资讯，请等待"));
                        }
                    }


                    @Override
                    public void onFailure(ResponeThrowable throwable) {
                        mBaseIView.getAdapter()
                                .setEmptyView(LayoutUtils.getNullLayout(mBaseIView.getContext(), R.drawable.null_message, "暂无资讯，请等待"));

                    }

                    @Override
                    public void onFinish() {
                    }
                });
    }

    @Override
    public void loadMore(String str) {
        ApiCreator.getApi().articleList(currentPage, DaoManager.getInstance().getUserBean().getId(), str)
                .compose(RxTransformer.<PagingModel<MessageModel>>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<PagingModel<MessageModel>>() {
                    @Override
                    protected void onSuccess(PagingModel<MessageModel> data) {
                        if (data != null && data.getList().size() > 0) {
                            mBaseIView.getAdapter().addData(data.getList());
                            if (currentPage >= totalPage) {
                                mBaseIView.getAdapter().loadMoreEnd();
                            } else {
                                mBaseIView.getAdapter().loadMoreComplete();
                            }
                            currentPage++;
                        } else {
                            mBaseIView.getAdapter().loadMoreEnd();
                        }
                    }

                    @Override
                    public void onFailure(ResponeThrowable throwable) {
//                        mBaseIView.getAdapter().loadMoreEnd();
                        mBaseIView.getAdapter().loadMoreFail();
                    }
                });
    }
}
